package com.yky.action.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import com.yky.action.base.BaseActionSupport;
import com.yky.entity.Project;
import com.yky.entity.ProjectReport;
import com.yky.entity.ProjectSummarize;
import com.yky.service.admin.ProjectReportService;
import com.yky.service.admin.ProjectService;
import com.yky.service.admin.ProjectSummarizeService;
import com.yky.utils.JsonUtil;
/**
 * 项目报告
 * @author vimesly
 * 
 */
@ParentPackage("perInterceptor")
@InterceptorRefs({@InterceptorRef("sessionStack")})
@Namespace(value = "/admin/report")
@Results( { @Result(name = "project_report", type = "redirect", location = "/admin/project/project_list.jsp") })
public class ReportAction extends BaseActionSupport {

	private static final long serialVersionUID = 1L;

	private String id;
	
	private String recordsDate; // 备案日期
	private String personRecruit; // 人员招募情况
	private String paperFilePath; // 论文发表情况
	private String annualReport; // 项目年度报告
	
	private String finishDate; // 完成日期
	private String finishSummarize; // 完成总结

	// 项目成果
	private String title; // 标题
	private String type; // 类型
	private String filePath; // 附件

	@Autowired
	private ProjectService projectService;

	@Autowired
	private ProjectReportService projectReportService;
	
	@Autowired
	private ProjectSummarizeService projectSummarizeService;
	
	
	public String findProject() throws Exception{
		if(null!=id){
			Project project=projectService.find(Integer.parseInt(id));
			
			Map<Object,Object> map=new HashMap<Object,Object>();
			map.put("name", project.getName());
			
			super.pringWriterToPage(JsonUtil.formatMapToJson(map));
		}
		return null;
	}
	public String findReport() throws Exception{
		if(null!=id){
			try{
				ProjectReport projectReport=projectReportService.find(Integer.parseInt(id));
				
				Map<Object,Object> map=new HashMap<Object,Object>();
				map.put("id", projectReport.getId());
				map.put("recordsDate", projectReport.getRecordsDate());
				map.put("personRecruit", projectReport.getPersonRecruit());
				map.put("paperFilePath", projectReport.getPaperFilePath());
				map.put("annualReport", projectReport.getAnnualReport());
				map.put("projectid", projectReport.getProject().getId());
				map.put("projectname", projectReport.getProject().getName());
				super.pringWriterToPage(JsonUtil.formatMapToJson(map));
			
			} catch (Exception e) {
				super.pringWriterToPage("error");
			}
		}
		return null;
	}
	public String findReportList() throws Exception{
		if(null!=id){
			List<Map<Object,Object>> list=new ArrayList<Map<Object,Object>>();
			List<ProjectReport> reportList=projectReportService.findByProperty("project.id",Integer.parseInt(id));
			if(null!=reportList){
				for (ProjectReport projectReport : reportList) {
					Map<Object,Object> map=new HashMap<Object,Object>();
					map.put("id", projectReport.getId());
					map.put("recordsDate", projectReport.getRecordsDate());
					map.put("personRecruit", projectReport.getPersonRecruit());
					map.put("paperFilePath", projectReport.getPaperFilePath());
					map.put("annualReport", projectReport.getAnnualReport());
					list.add(map);
				}
			}
			super.pringWriterToPage(JsonUtil.formatToJson(list));
		}
		return null;
	}
	
	public String saveReport() throws Exception{
		try{
			if(null!=id){
				Project project=projectService.find(Integer.parseInt(id));
				ProjectReport projectReport=new ProjectReport();
				
				projectReport.setRecordsDate(recordsDate);
				projectReport.setPersonRecruit(personRecruit);
				projectReport.setPaperFilePath(paperFilePath);
				projectReport.setAnnualReport(annualReport);
				
				projectReport.setProject(project);
				projectReportService.saveEntity(projectReport);	
				
				return "project_report";
			}
		}catch(Exception e){
			e.printStackTrace();
			super.pringWriterToPage("error");
		}
		return null;
	}
	
	public String saveSummarize() throws Exception{
		try{
			if(null!=id){
				Project project=projectService.find(Integer.parseInt(id));
				ProjectSummarize projectSummarize=new ProjectSummarize();
				
				projectSummarize.setFinishDate(finishDate);
				projectSummarize.setFinishSummarize(finishSummarize);
				projectSummarize.setTitle(title);
				projectSummarize.setType(type);
				projectSummarize.setFilePath(filePath);
				
				projectSummarize.setProject(project);
				projectSummarizeService.saveEntity(projectSummarize);
				
				return "project_report";
			}
		}catch(Exception e){
			e.printStackTrace();
			super.pringWriterToPage("error");
		}
		return null;
	}
	
	public String findSummarize() throws Exception{
		if(null!=id){
			try {
//				ProjectSummarize projectSummarize=projectSummarizeService.find(Integer.parseInt(id));
				List<ProjectSummarize> summarizeList=projectSummarizeService.findByProperty("project.id",Integer.parseInt(id));
				if(null!=summarizeList && summarizeList.size()>0){
					ProjectSummarize projectSummarize=summarizeList.get(0);
					
					Map<Object,Object> map=new HashMap<Object,Object>();
					map.put("id", projectSummarize.getId());
					map.put("finishDate", projectSummarize.getFinishDate());
					map.put("finishSummarize", projectSummarize.getFinishSummarize());
					map.put("title", projectSummarize.getTitle());
					map.put("type", projectSummarize.getType());
					map.put("filePath", projectSummarize.getFilePath());
					map.put("projectid", projectSummarize.getProject().getId());
					map.put("projectname", projectSummarize.getProject().getName());
					super.pringWriterToPage(JsonUtil.formatMapToJson(map));
				}
			} catch (Exception e) {
				super.pringWriterToPage("error");
			}

		}
		return null;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getRecordsDate() {
		return recordsDate;
	}

	public void setRecordsDate(String recordsDate) {
		this.recordsDate = recordsDate;
	}

	public String getPersonRecruit() {
		return personRecruit;
	}

	public void setPersonRecruit(String personRecruit) {
		this.personRecruit = personRecruit;
	}

	public String getPaperFilePath() {
		return paperFilePath;
	}

	public void setPaperFilePath(String paperFilePath) {
		this.paperFilePath = paperFilePath;
	}

	public String getAnnualReport() {
		return annualReport;
	}

	public void setAnnualReport(String annualReport) {
		this.annualReport = annualReport;
	}

	public ProjectService getProjectService() {
		return projectService;
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	public ProjectReportService getProjectReportService() {
		return projectReportService;
	}

	public void setProjectReportService(ProjectReportService projectReportService) {
		this.projectReportService = projectReportService;
	}
	public String getFinishDate() {
		return finishDate;
	}
	public void setFinishDate(String finishDate) {
		this.finishDate = finishDate;
	}
	public String getFinishSummarize() {
		return finishSummarize;
	}
	public void setFinishSummarize(String finishSummarize) {
		this.finishSummarize = finishSummarize;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getFilePath() {
		return filePath;
	}
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	public ProjectSummarizeService getProjectSummarizeService() {
		return projectSummarizeService;
	}
	public void setProjectSummarizeService(
			ProjectSummarizeService projectSummarizeService) {
		this.projectSummarizeService = projectSummarizeService;
	}
}
